/*
 * Main.java
 *
 * Created on April 16, 2007, 12:05 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package testnormalapp;

import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import usyd.comp5348.CatalogueInfo;
import usyd.comp5348.CatalogueException;
import usyd.comp5348.CatalogueRemote;
import usyd.comp5348.CustomerException;
import usyd.comp5348.CustomerInfo;
import usyd.comp5348.CustomerRemote;
import usyd.comp5348.OrderInfo;
import usyd.comp5348.OrderLineInfo;
import usyd.comp5348.OrderRemote;
import usyd.comp5348.utils.SettingsProvider;

/**
 *
 * @author Yuezhong Zhang
 */
public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            Properties env1 = new Properties();
            env1.setProperty("org.omg.CORBA.ORBInitialHost","192.168.1.40");
            env1.setProperty("org.omg.CORBA.ORBInitialPort","3700");
            InitialContext central = new InitialContext( env1 );
                        
            
            CatalogueRemote remote1 =(CatalogueRemote) central.lookup("ejb/Catalogue");
            CatalogueInfo c = new CatalogueInfo("catno1", "title1", "dir1", "gen1", 22.0f);
            try {
                int i = remote1.createVideo(c);
                System.out.println("i = " + i);
                c.setId(i);
                c.setDirector("dir2");
                remote1.modifyVideo(c);
                System.out.println("i = " + i);
                c = remote1.getVideoById(i);
                System.out.println("Director = " + c.getDirector());
                List<CatalogueInfo> ci = remote1.getVideosByDirector("dir2");
                for (CatalogueInfo d : ci) {
                    System.out.println("id = " + d.getId() + "." + d.getDirector());
                }
                
                System.out.println("Order test");
                CustomerInfo cinfo = new CustomerInfo("fred", "123 address");
                CustomerRemote custrem = (CustomerRemote)central.lookup("ejb/Customer");
                cinfo.setId(custrem.createCustomer(cinfo));
                OrderLineInfo [] oline = new OrderLineInfo[1];
                oline[0]=new OrderLineInfo(i, 10);
                OrderInfo oi = new OrderInfo(cinfo.getId(), 22.0f, oline, new Date(), "192.168.1.40", OrderInfo.SUBMITTED);
                
                
                Properties env2 = new Properties();
                env2.setProperty("org.omg.CORBA.ORBInitialHost","192.168.1.40");
                env2.setProperty("org.omg.CORBA.ORBInitialPort","3700");
                InitialContext remote = new InitialContext( env2 );
                central.addToEnvironment("org.omg.CORBA.ORBInitialHost","192.168.1.40");
                OrderRemote or = (OrderRemote)central.lookup("ejb/Order");
                try {
                    oi.setId(or.createOrder(oi));
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            } catch (CatalogueException ex) {
                ex.printStackTrace();
            } catch (CustomerException ex) {
                ex.printStackTrace();
            }
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
}
